package com.bw.a2207a.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider

/**
 *
 * @ClassName: BaseFragment2
 * @Description: java类作用描述
 * @Author: zhangyanchao
 * @CreateDate: 2024/10/17 11:14 AM
 * @Version: 1.0
 */
abstract class BaseFragment2<DB : ViewDataBinding, VM : ViewModel>(val layoutId: Int, val vm: VM) :
    Fragment() {

    lateinit var binding: DB
    lateinit var viewModel: VM
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DataBindingUtil.inflate(inflater, layoutId, container, false)
        binding.lifecycleOwner = this

        //获取viewModel 实例
        viewModel = ViewModelProvider(this).get(vm::class.java)
        initView()
        initData()
        return binding.root
    }

    abstract fun initData()

    abstract fun initView()

    override fun onDestroy() {
        super.onDestroy()
        binding.unbind()
    }
}